<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Stream Buffers</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.78.1" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, runtime, library" /><link rel="home" href="../index.html" title="The GNU C++ Library" /><link rel="up" href="io.html" title="Chapter 13.  Input and Output" /><link rel="prev" href="io.html" title="Chapter 13.  Input and Output" /><link rel="next" href="stringstreams.html" title="Memory Based Streams" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Stream Buffers</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="io.html">Prev</a> </td><th width="60%" align="center">Chapter 13. 
  Input and Output
  
</th><td width="20%" align="right"> <a accesskey="n" href="stringstreams.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="std.io.streambufs"></a>Stream Buffers</h2></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="io.streambuf.derived"></a>Derived streambuf Classes</h3></div></div></div><p>
    </p><p>Creating your own stream buffers for I/O can be remarkably easy.
      If you are interested in doing so, we highly recommend two very
      excellent books:
      <a class="link" href="http://www.angelikalanger.com/iostreams.html" target="_top">Standard C++
      IOStreams and Locales</a> by Langer and Kreft, ISBN 0-201-18395-1, and
      <a class="link" href="http://www.josuttis.com/libbook/" target="_top">The C++ Standard Library</a>
      by Nicolai Josuttis, ISBN 0-201-37926-0.  Both are published by
      Addison-Wesley, who isn't paying us a cent for saying that, honest.
   </p><p>Here is a simple example, io/outbuf1, from the Josuttis text.  It
      transforms everything sent through it to uppercase.  This version
      assumes many things about the nature of the character type being
      used (for more information, read the books or the newsgroups):
   </p><pre class="programlisting">
    #include &lt;iostream&gt;
    #include &lt;streambuf&gt;
    #include &lt;locale&gt;
    #include &lt;cstdio&gt;

    class outbuf : public std::streambuf
    {
      protected:
	/* central output function
	 * - print characters in uppercase mode
	 */
	virtual int_type overflow (int_type c) {
	    if (c != EOF) {
		// convert lowercase to uppercase
		c = std::toupper(static_cast&lt;char&gt;(c),getloc());

		// and write the character to the standard output
		if (putchar(c) == EOF) {
		    return EOF;
		}
	    }
	    return c;
	}
    };

    int main()
    {
	// create special output buffer
	outbuf ob;
	// initialize output stream with that output buffer
	std::ostream out(&amp;ob);

	out &lt;&lt; "31 hexadecimal: "
	    &lt;&lt; std::hex &lt;&lt; 31 &lt;&lt; std::endl;
	return 0;
    }
   </pre><p>Try it yourself!  More examples can be found in 3.1.x code, in
      <code class="filename">include/ext/*_filebuf.h</code>, and in this article by James Kanze:
      <a class="link" href="http://kanze.james.neuf.fr/articles/fltrsbf1.html" target="_top">Filtering
      Streambufs</a>.
   </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="io.streambuf.buffering"></a>Buffering</h3></div></div></div><p>First, are you sure that you understand buffering?  Particularly
      the fact that C++ may not, in fact, have anything to do with it?
   </p><p>The rules for buffering can be a little odd, but they aren't any
      different from those of C.  (Maybe that's why they can be a bit
      odd.)  Many people think that writing a newline to an output
      stream automatically flushes the output buffer.  This is true only
      when the output stream is, in fact, a terminal and not a file
      or some other device -- and <span class="emphasis"><em>that</em></span> may not even be true
      since C++ says nothing about files nor terminals.  All of that is
      system-dependent.  (The "newline-buffer-flushing only occurring
      on terminals" thing is mostly true on Unix systems, though.)
   </p><p>Some people also believe that sending <code class="code">endl</code> down an
      output stream only writes a newline.  This is incorrect; after a
      newline is written, the buffer is also flushed.  Perhaps this
      is the effect you want when writing to a screen -- get the text
      out as soon as possible, etc -- but the buffering is largely
      wasted when doing this to a file:
   </p><pre class="programlisting">
   output &lt;&lt; "a line of text" &lt;&lt; endl;
   output &lt;&lt; some_data_variable &lt;&lt; endl;
   output &lt;&lt; "another line of text" &lt;&lt; endl; </pre><p>The proper thing to do in this case to just write the data out
      and let the libraries and the system worry about the buffering.
      If you need a newline, just write a newline:
   </p><pre class="programlisting">
   output &lt;&lt; "a line of text\n"
	  &lt;&lt; some_data_variable &lt;&lt; '\n'
	  &lt;&lt; "another line of text\n"; </pre><p>I have also joined the output statements into a single statement.
      You could make the code prettier by moving the single newline to
      the start of the quoted text on the last line, for example.
   </p><p>If you do need to flush the buffer above, you can send an
      <code class="code">endl</code> if you also need a newline, or just flush the buffer
      yourself:
   </p><pre class="programlisting">
   output &lt;&lt; ...... &lt;&lt; flush;    // can use std::flush manipulator
   output.flush();               // or call a member fn </pre><p>On the other hand, there are times when writing to a file should
      be like writing to standard error; no buffering should be done
      because the data needs to appear quickly (a prime example is a
      log file for security-related information).  The way to do this is
      just to turn off the buffering <span class="emphasis"><em>before any I/O operations at
      all</em></span> have been done (note that opening counts as an I/O operation):
   </p><pre class="programlisting">
   std::ofstream    os;
   std::ifstream    is;
   int   i;

   os.rdbuf()-&gt;pubsetbuf(0,0);
   is.rdbuf()-&gt;pubsetbuf(0,0);

   os.open("/foo/bar/baz");
   is.open("/qux/quux/quuux");
   ...
   os &lt;&lt; "this data is written immediately\n";
   is &gt;&gt; i;   // and this will probably cause a disk read </pre><p>Since all aspects of buffering are handled by a streambuf-derived
      member, it is necessary to get at that member with <code class="code">rdbuf()</code>.
      Then the public version of <code class="code">setbuf</code> can be called.  The
      arguments are the same as those for the Standard C I/O Library
      function (a buffer area followed by its size).
   </p><p>A great deal of this is implementation-dependent.  For example,
      <code class="code">streambuf</code> does not specify any actions for its own
      <code class="code">setbuf()</code>-ish functions; the classes derived from
      <code class="code">streambuf</code> each define behavior that "makes
      sense" for that class:  an argument of (0,0) turns off buffering
      for <code class="code">filebuf</code> but does nothing at all for its siblings
      <code class="code">stringbuf</code> and <code class="code">strstreambuf</code>, and specifying
      anything other than (0,0) has varying effects.
      User-defined classes derived from <code class="code">streambuf</code> can
      do whatever they want.  (For <code class="code">filebuf</code> and arguments for
      <code class="code">(p,s)</code> other than zeros, libstdc++ does what you'd expect:
      the first <code class="code">s</code> bytes of <code class="code">p</code> are used as a buffer,
      which you must allocate and deallocate.)
   </p><p>A last reminder:  there are usually more buffers involved than
      just those at the language/library level.  Kernel buffers, disk
      buffers, and the like will also have an effect.  Inspecting and
      changing those are system-dependent.
   </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="io.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="io.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="stringstreams.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 13. 
  Input and Output
  
 </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Memory Based Streams</td></tr></table></div></body></html>